package com.zbkj.service.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.zbkj.common.model.system.SystemGroupData;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.SystemFormItemCheckRequest;
import com.zbkj.common.request.SystemGroupDataRequest;
import com.zbkj.common.request.SystemGroupDataSearchRequest;
import com.zbkj.common.utils.CrmebUtil;
import com.zbkj.service.dao.SystemGroupDataDao;
import com.zbkj.service.service.SystemGroupDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class SystemGroupDataServiceImpl extends ServiceImpl<SystemGroupDataDao, SystemGroupData> implements SystemGroupDataService {
    @Resource
    private SystemGroupDataDao dao;

    @Override
    public List<SystemGroupData> getList(SystemGroupDataSearchRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<SystemGroupData> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        SystemGroupData model = new SystemGroupData();
        BeanUtils.copyProperties(request, model);
        lambdaQueryWrapper.setEntity(model);
        lambdaQueryWrapper.orderByAsc(SystemGroupData::getSort).orderByAsc(SystemGroupData::getId);
        return dao.selectList(lambdaQueryWrapper);
    }

    @Override
    public Boolean create(SystemGroupDataRequest systemGroupDataRequest) {
//        systemFormTempService.check
        return null;
    }

    @Override
    public Boolean update(Integer id, SystemGroupDataRequest request) {
        return null;
    }

    @Override
    public <T> List<T> getListByGid(Integer gid, Class<T> cls) {
        SystemGroupDataSearchRequest systemGroupDataSearchRequest = new SystemGroupDataSearchRequest();
        systemGroupDataSearchRequest.setGid(gid);
        systemGroupDataSearchRequest.setStatus(true);
        List<SystemGroupData> list = getList(systemGroupDataSearchRequest,new PageParamRequest());

        List<T> arrayList = new ArrayList<>();
        if(list.size() < 1){
            return null;
        }

        for (SystemGroupData systemGroupData : list) {
            JSONObject jsonObject = JSON.parseObject(systemGroupData.getValue());
            List<SystemFormItemCheckRequest> systemFormItemCheckRequestList = CrmebUtil.jsonToListClass(jsonObject.getString("fields"), SystemFormItemCheckRequest.class);
            if(systemFormItemCheckRequestList.size() < 1){
                continue;
            }
            HashMap<String,Object> map = new HashMap<>();
            T t;
            for( SystemFormItemCheckRequest systemFormItemCheckRequest : systemFormItemCheckRequestList){
                map.put(systemFormItemCheckRequest.getName(),systemFormItemCheckRequest.getValue());
            }
            map.put("id",systemGroupData.getId());
            t = CrmebUtil.mapToObj(map,cls);
            arrayList.add(t);
        }
        return arrayList;
    }

    @Override
    public List<HashMap<String, Object>> getListMapByGid(Integer gid) {
        return null;
    }

    @Override
    public <T> T getNormalInfo(Integer groupDataId, Class<T> cls) {
        return null;
    }

    @Override
    public HashMap<String, Object> getMenuUser() {
        return null;
    }

    @Override
    public List<SystemGroupData> findListByGid(Integer gid) {
        return null;
    }

    @Override
    public Boolean deleteByGid(Integer gid) {
        return null;
    }
}
